from fastapi import FastAPI, Request, Response
from math import ceil

from common.exception.errors import HTTPError
from common.response.response_code import HttpCode

async def http_limit_callback(request: Request, response: Response, expire: int):
    """请求限制时的默认回调函数

    Args:
        request (Request): 
        response (Response): 
        expire (int): 剩余毫秒

    Raises:
        HTTPError: 
    """
    
    expires = ceil(expire / 1000)
    raise HTTPError(code=HttpCode.HTTP_429, msg='请求过于频繁，请稍后重试', headers={'Retry-After': str(expires)})